<?php
/*
 * Created on Feb 5, 2009
 * @author: ANH
 * @PARAM rtype is rootType: example: caves is type then attraction is rtype
 * @filesource: modules/place/controllers/PlaceController.php
 */

class Node_MasterController extends Clu_Controller_Action_Master
{
	public function init()
	{
//		//require_once  (APP_PATH.'/modules/article/Model/Article.php');
//		//require_once  (APP_PATH.'/modules/comment/Model/Comment.php');
//		//require_once  (APP_PATH.'/modules/user/Model/User.php');
//		$this->mArticle = new article_Model_Service();
//		$this->mCategory->setTables('place');

		//require_once (APP_PATH.'/modules/node/Model/NodeService.php');
		//require_once (APP_PATH.'/modules/category/Model/Category.php');
		
		$this->m = new node_Model_NodeService();
		$this->mCategory = new category_Model_Category();
		$this->mService = new node_Model_NodeService();
		parent::init();
	}
	
	public function newAction()
	{
		if (!$nodeType = $this->_request->getParam('type'))
		{
		    $this->presenceIdentifier = 2; //render X--master--choose-node-type.phtml
		    $this->viewParams['mode'] = 'new';
            return;
		}
		
		$sess = new Zend_Session_Namespace(CLU_NS);
		if (strpos ($sess->userPermissions, 'create ' . $nodeType) === false)
        {
            $this->renderError(null,t(68));
            return;
        }

		//nodeType info for what to create in the form (and the 
		$nodeTypeInfo = $this->mService->getNodeTypeInfo($nodeType);
		if (!$nodeTypeInfo)
		{
			$this->viewParams['notificationType'] = "node-type-not-exists";
			$this->viewParams['nodeType'] = $nodeType;
			$this->presenceIdentifier = 3 ; //nodeType doesn't exist yet and let's try create
			return;
		}
        
        // allowed, so let's create a node_Form_Node first, regardless of POST or GET
		$this->viewParams['form'] = new node_Form_Node();
		$this->viewParams['type'] = $nodeType; 
		$this->viewParams['nodeTypeInfo'] = $nodeTypeInfo;
		$this->viewParams['form']->cluBuild(array(
							'action' => 'new',
                    		'type' => $nodeType,
							'nodeTypeInfo' => $nodeTypeInfo,
                    		'userPermissions' => $sess->userPermissions,
                    		'language' => $sess->viewLang
		));
		
		$data = $this->_request->getParams();
		if ($this->_request->isPost() && $this->viewParams['form']->isValid($data))
		{
			//set node table and categories
			$this->mService->mNode->setNodeTableName($nodeTypeInfo['node_table_name']);
			$ret = $this->mService->insertNewNode($data, $nodeTypeInfo);
			if ($ret['success'])
			{
				if ($nodeType == "gallery")
				header ( "Location: " .
						$this->view->hvnUrl(array(
								'module' => 'gallery',
								'action' => 'upload-photos',
						) ,
						'nocontroller') .
						 "?id=". $ret['lastInsertId']  
				);
				
				else if ($nodeType == "news" || $nodeType == "blog")
				header ( "Location: " .
						$this->view->hvnUrl(array(
								'module' => 'node',
								'controller' => 'master',
								'type' => $nodeType,
								'action' => 'thumbnail',
								'operation' => "new",
						) ,
						'node') .
						 "?id=". $ret['lastInsertId']  
				);
				else
				header ( "Location: " .
						$this->view->hvnUrl(array(
								'module' => 'node',
								'controller' => 'master',
								'type' => $nodeType,
								'action' => 'thumbnail',
								'operation' => "new",
						) ,
						'node') .
						 "?operation=new&id=". $ret['lastInsertId']  
				);
				
				//redirect to a success page to avoid refresh
				return;
			}	 
			else
			{
				$this->viewParams['errorMessage'] = $ret['errorMessage'];
			}
		}
		elseif ($this->_request->isPost()) 
		{
				$this->viewParams['errorMessage'] = "Input not valid";
		}
		else //GET request, first time insert, so let's insert default values
		{
			//populate the form
			// at least we need to get category here
//			$this->viewParams['form']->populateCategory($nodeTypeInfo);
			//TODO: is this default data from $nodeTypeInfo??
			//$this->viewParams['form']->populateDefault(array());
		
		}	
		
		// either form not VALID or insert failed or we are first time
		// in all cases, we need to populate the form and display it
		//$this->viewParams['form']->populateCategory() or DistrictId
		
		//extra variables for the master--new.phtml
//		die_a($this->viewParams);
		$this->viewParams['mode'] = 'new'; 
	}
	
	public function editAction()
	{

		//need to have nodeTypeInfo first
		//to determine which node table ("nodes", "news"..) to work on
		if (!$nodeType = $this->_request->getParam('type'))
		{
			$this->renderError(null, __("Node Type not specified"));
            return;
		}

		//nodeType info for what to create in the form (and the 
		$nodeTypeInfo = $this->mService->getNodeTypeInfo($nodeType);
		if (!$nodeTypeInfo)
		{
			$this->renderError(null, "Node Type " . $nodeType ." does not exist ");
			return;
		}
		if (!$nodeTypeInfo['is_editable'])
		{
			$this->renderError(null, "Node Type " . $nodeType ." not editable ");
			return;
		}

		//new view the node
		$nodeId = $this->_request->getParam('id');
		$node = $this->mService->findNodeByPrimary($nodeTypeInfo, $nodeId);
		if (!$node)
		{
			$this->renderError(null, "Node content id ". $nodeId . " not found");
        	return;
		}
		//check permission if user is allowed to edit the node
		$sess = new Zend_Session_Namespace(CLU_NS);
		if (!isset($sess->userId))
		{
		 	$this->renderError(null,t(726));
        	return;	
		}
		
		if (($sess->userId != $node->poster_id && strpos($sess->userPermissions, 'update ' . $nodeType) === false) ||
				($sess->userId == $node->poster_id && strpos ($sess->userPermissions, 'update own ' . $nodeType) === false))
		{
		 	$this->renderError(null,t(726));
        	return;	
		}
		//create the form and populate
		// allowed, so let's create a node_Form_Node first, regardless of POST or GET
        // allowed, so let's create a node_Form_Node first, regardless of POST or GET
		$this->viewParams['form'] = new node_Form_Node();
		$this->viewParams['type'] = $nodeType; 
		$this->viewParams['nodeTypeInfo'] = $nodeTypeInfo;
		$this->viewParams['form']->cluBuild(array(
							'action' => 'new',
                    		'type' => $nodeType,
							'nodeTypeInfo' => $nodeTypeInfo,
                    		'userPermissions' => $sess->userPermissions,
                    		'language' => $sess->viewLang
		));
		
		$data = $this->_request->getParams();
		if ($this->_request->isPost() && $this->viewParams['form']->isValid($data))
		{
			//set node table and categories
			$this->mService->mNode->setNodeTableName($nodeTypeInfo['node_table_name']);
			$ret = $this->mService->updateNode($data, $nodeTypeInfo, $node);
			if ($ret['success'])
			{
				$this->viewParams['successMessage'] = "Updated OK";
			}	 
			else
			{
				$this->viewParams['errorMessage'] = $ret['errorMessage'];
			}
		}
		elseif ($this->_request->isPost()) 
		{
				$this->viewParams['errorMessage'] = "Input not valid";
		}
		else //GET request, first time insert, so let's insert default values
		{
			$this->viewParams['form']->populate($node->toArray());
		}	
		if (isset($ret['node']))
			$this->viewParams['node'] = $ret['node'];
		else
			$this->viewParams['node'] = $node;
		$this->viewParams['nodeTypeInfo'] = $nodeTypeInfo;
		$this->viewParams['mode'] = 'edit';

		$this->paramsForWidgets['node_type'] = $nodeType;
	}
	
	
	public function viewAction()
	{
		if (!$nodeType = $this->_request->getParam('type'))
		{
			$this->renderError(null, "Node Type does not exist");
            return;
		}
		//$nodeType = 'news';
		$sess = new Zend_Session_Namespace(CLU_NS);
		//nodeType info for what to create in the form (and the 
		$nodeTypeInfo = $this->mService->getNodeTypeInfo($nodeType);
		if (!$nodeTypeInfo)
		{
			$this->renderError(null, "Node Type does not exist");
		}
		//new view the node
		$nodeId = $this->_request->getParam('id');
		$ret = $this->mService->getNodeData($nodeTypeInfo, $nodeId, array('get_article' => 1));
		if ($ret['success'])
		{
			$this->viewParams['node'] = $ret['node'];
			if ($nodeTypeInfo['needs_translation'] && $sess->viewLang != get_language_from_int($ret['node']['language']))
				$this->viewParams['article'] = $ret['article'];
			    $this->viewParams['nodeTypeInfo'] = $nodeTypeInfo;
			    $this->mService->updateDbAfterView($nodeTypeInfo, $nodeId);
		}
		
		$sess = new Zend_Session_Namespace(CLU_NS);
		$db = $this->db;		
		$sqlNext = "SELECT n.id from nodes n WHERE n.poster_id = ".$this->viewParams['node']['poster_id']." AND n.id > " . $nodeId . " ORDER BY n.id ASC limit 1";
		$nodeNext = $this->db->fetchAll($sqlNext); 
		
		$sqlBack = "SELECT n.id from nodes n WHERE n.poster_id = ".$this->viewParams['node']['poster_id']." AND n.id < " . $nodeId . " ORDER BY n.id DESC limit 1";
		$nodeBack = $this->db->fetchAll($sqlBack); 
		
		
		$this->viewParams['node_next'] = $nodeNext;
		
		$this->viewParams['node_back'] = $nodeBack;
		
		$this->paramsForWidgets["id"] = $nodeId;
		$this->paramsForWidgets["content_id"] = $nodeId;
		$this->paramsForWidgets["content_poster_id"] = $ret['node']['poster_id'];
		$this->paramsForWidgets["content_type"] = $nodeType;
		$this->paramsForWidgets["type"] = $nodeType;
		$this->paramsForWidgets["b"] = 4;
		$this->paramsForWidgets["category"] = $ret['node']['category'];
//		die_a($this->paramsForWidgets);
		if (isset($ret['node']['gallery']))
			$this->paramsForWidgets['gallery_id'] = $ret['node']['gallery'];
		else 
			$this->paramsForWidgets['gallery_id'] = null;
		//die_a($ret);        
		// TODO : if gallery, then add the gallery 
	}
	
	public function searchAction()
	{
		if (!$nodeType = $this->_request->getParam('type'))
		{
			die("No node Type specified");
			//$this->presenceIdentifier = 2; //choose a node type first
			return;
		}
         
		$sess = new Zend_Session_Namespace(CLU_NS);
		$nodeTypeInfo = $this->mService->getNodeTypeInfo($nodeType);

		if (!$nodeTypeInfo)
		{
			$this->renderError(null, "Node Type does not exist");
		}
		$this->viewParams['form'] = new node_Form_NodeSearch();
		
		$this->viewParams['form']->cluBuild(array(
							'action' => 'search',
                    		'type' => $nodeType,
							'nodeTypeInfo' => $nodeTypeInfo,
                    		'userPermissions' => $sess->userPermissions,
                    		'language' => $sess->viewLang,
							'searchAll' => $this->_request->getParam('searchAll')
							));
							
		$this->viewParams['nodeTypeInfo'] = $nodeTypeInfo;
		$this->viewParams['type'] = $nodeType;
		$requestData = $this->_request->getParams();
		 
		if (isset($requestData['cluSearch']) || $this->_request->isPost())
		{
			$tmp = $this->mService->prepareNodeSearchData($requestData, $nodeTypeInfo);
			if (!$tmp['success'])
			{
				$this->renderError(null, $tmp['errorMessage']);
				return;
			}
			
			$searchData = $tmp['nodeSearchData']; 
			$searchData['searchAll'] = 1;
			$searchData['cluSearch'] = 1;
			$searchUrl = encode_search_url($searchData, $nodeType);
			$this->viewParams['searchUrl'] = 
			"http://" . SITE_URL . $this->view->hvnUrl(array('action' => 'search', 'type' => $nodeType ),'node') . "?$searchUrl";
			//die_a($searchData);
		   	$selectStmt = $this->mService->search($searchData, $nodeTypeInfo);
		   	//die_a($this->db->fetchAll($selectStmt));
			$paginator = $this->generatePaginator($selectStmt, $searchData['page'], $searchData['items_per_page'], 7, 'Jumping');
			$this->viewParams['form']->populate($requestData);
		}

        if(isset($paginator))
        {	
        	if (strpos($sess->userPermissions, "administer $nodeType"))
        	{
		       	$this->viewParams['paginator'] = $paginator;
				$this->viewParams['pList'] = $paginator->getCurrentItems();
        	}
        	else
        	{			
		       	$this->viewParams['paginator'] = $paginator;
				$this->viewParams['items'] = $paginator->getCurrentItems();
				//die_a($this->viewParams['pList']);
				//get articles
				//require_once(APP_PATH . "/modules/article/Model/Article.php");
				$articleList = array();
				$mArticle = new article_Model_Service();
				foreach ($this->viewParams['items'] as $p)
				{
					$articleList[$p['id']] = $mArticle->getArticleData($p['article_id']);
				}
				$this->viewParams['aList'] = $articleList;
        	}
       	}
       	
//		$this->paramsForWidgets['id'] = $id;
//		$this->viewParams['id'] = $id;
		$this->viewParams['contentType'] = $nodeType;
		if(isset($searchData['page']))
			$this->paramsForWidgets['page']=$searchData['page'];
        
        //TODO : user different layouts for different user roles
        //change presence_identifier
        
        if (strpos($sess->userPermissions, 'administer ' .$nodeType))
        {
        	$this->presenceIdentifier = 2; //render
        }
	}
	

	public function newNodeTypeAction()
	{
		$sess = new Zend_Session_Namespace(CLU_NS);
		if (strpos ($sess->userPermissions, 'administer everything') === false)
        {
            $this->renderError(null,t(68));
            return;
        }

        // allowed, so let's create a node_Form_Node first, regardless of POST or GET
		$this->viewParams['form'] = new node_Form_NodeType();
		$this->viewParams['mode'] = 'new';
		if ($this->_request->isPost())
    		$this->viewParams['form']->cluBuild(array(
    							'action' => 'new-node-type'));
		else
			$this->viewParams['form']->cluBuild(array(
    							'action' => 'new-node-type', 'isFirstTime' => 1));
		
		//Form done, let's insert
    	$data = $this->_request->getParams();
		if ($this->_request->isPost() && $this->viewParams['form']->isValid($data))
		{
    		$metadataText = $this->_request->getParam('metadata');
    		eval ($metadataText);
    		$ret = $this->mService->insertNewNodeType(array('type' => $data['type'], 'metadata' => serialize($config)));
    		if ($ret['success'] == false)
    			$this->viewParams['errorMessage'] = $ret['errorMessage'];
		}
	}
	
	
	public function editNodeTypeAction()
	{
		$sess = new Zend_Session_Namespace(CLU_NS);
		if (strpos ($sess->userPermissions, 'administer everything') === false)
        {
            $this->renderError(null,t(68));
            return;
        }

        // allowed, so let's create a node_Form_Node first, regardless of POST or GET
        $nodeType = $this->_request->getParam('type');
        if (!$nodeTypeInfo = $this->mService->getNodeTypeInfo($nodeType))
        	$this->renderError(null, "Cannot find node type to edit");
        	
		$this->viewParams['form'] = new node_Form_NodeType();
   		$this->viewParams['form']->cluBuild(array(
    							'action' => 'edit-node-type'));
    	$data = $this->_request->getParams();

    	if ($this->_request->isPost() && $this->viewParams['form']->isValid($data))
		{
    		$metadataText = $this->_request->getParam('metadata');
    		eval ($metadataText);
    		$this->mService->saveNodeType(array('type' => $data['type'], 'metadata' => serialize($config)));
    		header("Location: " . $this->view->hvnUrl(array("module" => 'node', 'action' => 'success'), 'nocontroller'));
		}
		elseif (!$this->_request->isPost()) //first time
		{
			//populate the form
			$this->viewParams['form']->populate(array(
					"type" => $nodeTypeInfo['type'],
					"metadata" => $this->viewParams['form']->transformArrToText($nodeTypeInfo)
			));
		}
	}
	
	public function listNodeTypesAction()
	{
		$sess = new Zend_Session_Namespace(CLU_NS);
		if (strpos ($sess->userPermissions, 'administer everything') === false)
        {
            $this->renderError(null,t(68));
            return;
        }
        $ret = $this->mService->getNodeTypeList();
        
        if ($ret['success'])
        {
        	$this->viewParams['nodeTypes'] = $ret['nodeTypes'];
        }
        else
        	$this->viewParams['errorMessage'] = $ret['errorMessage'];
        
	}
	
	public function successAction()
	{
		$this->viewParams['type'] = $this->_request->getParam('type');
		$this->viewParams['operation'] = $this->_request->getParam('operation');
		if ($this->viewParams['operation'] == 'new')
		{
			$this->viewParams['lastInsertId'] = $this->_request->getParam('lastInsertId');
		}
	}
	
	public function manageFeaturedNodeAction()
	{
		if (!$nodeType = $this->_request->getParam('type'))
		{
		    $this->presenceIdentifier = 2; //render X--master--choose-node-type.phtml
		    return;
		}
		
		$sess = new Zend_Session_Namespace(CLU_NS);
		if (strpos ($sess->userPermissions, 'create ' . $nodeType) === false)
        {
            $this->renderError(null,t(68));
            return;
        }
		//nodeType info for what to create in the form (and the 
		$nodeTypeInfo = $this->mService->getNodeTypeInfo($nodeType);
		$requestData = array('status' => 1);
	   	$selectStmt = $this->mService->search($requestData, $nodeTypeInfo);
		$this->viewParams['displayData'] = $this->db->fetchAll($selectStmt);
		$this->viewParams['type'] = $nodeType;
	}
	
	public function galleryAction()
	{
	
		//need to have nodeTypeInfo first
		//to determine which node table ("nodes", "news"..) to work on
		if (!$nodeType = $this->_request->getParam('type'))
		{
			$this->renderError(null, __("Node Type not specified"));
            return;
		}

		//nodeType info for what to create in the form (and the 
		$nodeTypeInfo = $this->mService->getNodeTypeInfo($nodeType);
		if (!$nodeTypeInfo)
		{
			$this->renderError(null, "Node Type " . $nodeType ." does not exist ");
			return;
		}
		if (!isset($nodeTypeInfo['has_gallery']) || !$nodeTypeInfo['has_gallery'])
		{
			$this->renderError(null, "Node Type " . $nodeType ." does not have a gallery ");
			return;
		}

		//new view the node
		$nodeId = $this->_request->getParam('id');
		$node = $this->mService->findNodeByPrimary($nodeTypeInfo, $nodeId);
		if (!$node)
		{
			$this->renderError(null, "Node content id ". $nodeId . " not found");
        	return;
		}

		//check permission if user is allowed to edit the node
		$sess = new Zend_Session_Namespace(CLU_NS);
		if (!isset($sess->userId))
		{
		 	$this->renderError(null,t(726));
        	return;	
		}
		
		if (($sess->userId != $node->poster_id && strpos($sess->userPermissions, 'update ' . $nodeType) === false) ||
				($sess->userId == $node->poster_id && strpos ($sess->userPermissions, 'update own ' . $nodeType) === false))
		{
		 	$this->renderError(null,t(726));
        	return;	
		}	
		
		$this->paramsForWidgets['node_type'] = $nodeType;
		//pas the gallery id to the gallery/widget/upload-photos        
        if ($node->gallery)
        {
			$this->paramsForWidgets["id"] = $node->gallery;
        }
        else
        {
        	$this->presenceIdentifier = 2; //Create a new gallery message
        	$this->paramsForWidgets["content_id"] = $nodeId;
        	$this->paramsForWidgets["content_type"] = "node";
        }
	}

	
	public function thumbnailAction()
    {
		//need to have nodeTypeInfo first
		//to determine which node table ("nodes", "news"..) to work on
		if (!$nodeType = $this->_request->getParam('type'))
		{
			$this->renderError(null, __("Node Type not specified"));
            return;
		}

		//nodeType info for what to create in the form (and the 
		$nodeTypeInfo = $this->mService->getNodeTypeInfo($nodeType);
		if (!$nodeTypeInfo)
		{
			$this->renderError(null, "Node Type " . $nodeType ." does not exist ");
			return;
		}
		if (!isset($nodeTypeInfo['has_thumb']) || !$nodeTypeInfo['has_thumb'])
		{
			$this->renderError(null, "Node Type " . $nodeType ." does not have a thumbnail");
			return;
		}

		//new view the node
		$nodeId = $this->_request->getParam('id');
		$node = $this->mService->findNodeByPrimary($nodeTypeInfo, $nodeId);
		if (!$node)
		{
			$this->renderError(null, "Node content id ". $nodeId . " not found");
        	return;
		}

		//check permission if user is allowed to edit the node
		$sess = new Zend_Session_Namespace(CLU_NS);
		if (!isset($sess->userId))
		{
		 	$this->renderError(null,t(726));
        	return;	
		}
		
		if ((strpos($sess->userPermissions, 'update ' . $nodeType) === false) &&
				($sess->userId == $node->poster_id && strpos ($sess->userPermissions, 'update own ' . $nodeType) === false))
		{
			
		 	$this->renderError(null,t(726));
        	return;	
		}	
		
		//permission checked
		$this->paramsForWidgets['node_type'] = $nodeType;
		$this->viewParams['nodeType'] = $nodeType;
		$this->viewParams['id'] = $nodeId;
		if ($this->_request->isPost())
		{
			 $fileName = $_FILES['myFile']['name'];
			 if($_FILES['myFile']['name']){
				$posterId = $node['poster_id'];
				$ustoreDir = APP_PATH."/public/ustore/$posterId/images/original/$nodeType/$nodeId/thumb/";
				if (!file_exists($ustoreDir))
							mkdir($ustoreDir, 0777  , true);
				$ufilesDir = APP_PATH."/public/ufiles/$posterId/images/original/$nodeType/$nodeId/thumb/" ;
				if (!file_exists($ufilesDir))
							mkdir($ufilesDir, 0777  , true);
				$fileName = $_FILES['myFile']['name'];
				//make sure the file name is unique
				$targetFile = $ustoreDir . $fileName;
				$i = 1;
				while(file_exists($targetFile))
				{
								$fileName = $i . "_" . $_FILES['myFile']['name'];
								$targetFile = $ustoreDir . $fileName;	
								$i++;
				};
				move_uploaded_file($_FILES['myFile']['tmp_name'],$targetFile);
				//require_once APP_PATH."/library/common.php";
				$watermarkDir = APP_PATH."/public/watermark/";
				// now generate copies
				//TODO: if editors, then do watermark
				$ret = generate_image_copies ("ustore", $ustoreDir,$fileName, $watermarkDir, null, null, $posterId, false); //with wm		
				if ($ret == false)
				{
					//remove the uploaded files
			    unlink ($targetFile);
					exit();
			        }
			   else
			   {
			        	//$this->viewParams['thumb'] = $targetFile;
			        	$thumb = str_replace(APP_PATH, "", $targetFile);
			        	$node->thumb = str_replace("/public/ustore/", "/public/ufiles/", $thumb);
			        	try 
			        	{
			        		$node->save();
			        	}
			        	catch (Exception $e)
			        	{
			        		die($e->getMessage());
			        	}
			   }
			}
		 }
		 if (!is_null($node->thumb))
			$this->viewParams['thumb'] = $node->thumb;
		 
		//$this->viewParams['place'] = $place;
    }  
    	
	public function blogHomeAction()
	{
	//	if($this->issetParam()){
		$this->paramsForWidgets["page"] = $this->_request->getParam('page');
		//}
		if($data = $this->_request->getParam('b'))
		{
			$this->paramsForWidgets["b"] = $data;
			$this->presenceIdentifier = 2;
			return;
		}
	}
	
	public function viewCategoryAction()
	{
		if (!$nodeType = $this->_request->getParam('type'))
		{
			$this->renderError(null, "Node Type does not exist");
            return;
		}
		//$nodeType = 'news';
		$sess = new Zend_Session_Namespace(CLU_NS);
		//nodeType info for what to create in the form (and the 
		$nodeTypeInfo = $this->mService->getNodeTypeInfo($nodeType);
		if (!$nodeTypeInfo)
		{
			$this->renderError(null, "Node Type does not exist");
		}
		//new view the node
		$nodeCategory = $this->_request->getParam('category');
		$sql = "SELECT * from " . $nodeType . " WHERE category = " . $nodeCategory . " ORDER BY created ASC";
		$temp = $this->db->fetchAll($sql); 
		$nodeId = $temp['0']['id'];//$this->_request->getParam('id');
		$ret = $this->mService->getNodeData($nodeTypeInfo, $nodeId, array('get_article' => 1));
		if ($ret['success'])
		{
			$this->viewParams['node'] = $ret['node'];
			if ($nodeTypeInfo['needs_translation'] && $sess->viewLang != get_language_from_int($ret['node']['language']))
				$this->viewParams['article'] = $ret['article'];
			$this->viewParams['nodeTypeInfo'] = $nodeTypeInfo;
			$this->mService->updateDbAfterView($nodeTypeInfo, $nodeId);
		}
		$this->paramsForWidgets["id"] = $nodeId;
		$this->paramsForWidgets["content_id"] = $nodeId;
		$this->paramsForWidgets["content_type"] = $nodeType;
		$this->paramsForWidgets["type"] = $nodeType;
		$this->paramsForWidgets["category"] = $nodeCategory;
		
		
		if (isset($ret['node']['gallery']))
			$this->paramsForWidgets['gallery_id'] = $ret['node']['gallery'];
		else 
			$this->paramsForWidgets['gallery_id'] = null;
		//die_a($ret);        
		// TODO : if gallery, then add the gallery 
	}
	
	
}
?>
